package cn.t.facade.order;

import cn.t.constants.ParamConstants;
import cn.t.constants.ResponseError;
import cn.t.core.exception.BizException;
import cn.t.dto.data.CustomerPickupData;
import cn.t.dto.data.WorkOrderProcessResultMq;
import cn.t.model.order.OrderDO;
import cn.t.model.order.OrderDetailDO;
import cn.t.model.order.OverPickupRecordDO;
import cn.t.model.user.CustomerFirmDO;
import cn.t.model.user.UserQueryDO;
import cn.t.service.user.CustomerFirmService;
import cn.t.service.user.UserQueryService;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


/**
 * @author l
 */
@Service
@Slf4j
public class SynchronizeWorkOrderFacade {
    @Resource
    private UserQueryService userQueryService;
    @Resource
    private CustomerFirmService customerFirmService;





    /**
     * 转单时验证是否需要处理工单（领用订单是否关联有工单）
     */
    public void synchronizeWorkOrder(OrderDO orderBean, List<OrderDetailDO> details, List<OverPickupRecordDO> overPickup) {
        boolean isOverPickup= !CollectionUtils.isEmpty(overPickup);

        //查询公司code信息
        UserQueryDO userQueryDO = new UserQueryDO();
        userQueryDO.setId(orderBean.getUserId());
        userQueryDO.setFirmId(orderBean.getFirmId());
        CustomerFirmDO result = customerFirmService.isExistedFirm(orderBean.getFirmId());
   /*     if (ObjectUtils.isEmpty(result) || !Objects.equals(ParamConstants.PUSHCUSTOMERCODE, result.getFirmCode())) {
            return;
        }*/

        Map<String, Integer> orderDetailsMap = details.stream().collect(Collectors.groupingBy(OrderDetailDO::getSkuCode, Collectors.summingInt(OrderDetailDO::getQty)));
        if (CollectionUtils.isEmpty(orderDetailsMap)){
            throw new BizException(ResponseError.ORDER_GOODS_NOT_FOUND);
        }

        //推送到mqtt
        WorkOrderProcessResultMq workOrderProcessResultMq = new WorkOrderProcessResultMq();
        List<CustomerPickupData> customerPickupDataList = new ArrayList<>();
        String overPick = String.valueOf(ParamConstants.INT_0);
        if (isOverPickup) {
            overPick = String.valueOf(ParamConstants.INT_1);
        }
        workOrderProcessResultMq.setIsOverPickup(overPick);
        //推送数据组装
        orderDetailsMap.forEach((skuCode, qty) -> {
            CustomerPickupData customerPickupData = new CustomerPickupData();
            customerPickupData.setSku(skuCode);
            customerPickupData.setQty(qty);

                customerPickupData.setItemId(skuCode);
            customerPickupDataList.add(customerPickupData);
        });
        UserQueryDO userInfo = userQueryService.findByUserId(orderBean.getFirmId(), orderBean.getUserId());
        if (userInfo != null){
            workOrderProcessResultMq.setCreatorId(userInfo.getEmployeeCode());
        }

        workOrderProcessResultMq.setActionTime(new Date());
        workOrderProcessResultMq.setWorkOrderSn(orderBean.getWorkOrderSn());
        workOrderProcessResultMq.setDeviceId(String.valueOf(orderBean.getDeviceId()));
        workOrderProcessResultMq.setPickupData(customerPickupDataList);
        //推送数据
        try {
            log.info("发送工单处理消息{}", new ObjectMapper().writeValueAsString(workOrderProcessResultMq));
        } catch (JsonProcessingException e) {
            log.warn("发送工单处理消息参数转换异常 {}", e.getMessage());
        }
        //email
    }


}
